package com.qing.design.patterns.decorator.example.decorators;

import java.io.*;

/**
 * 简单数据读取器
 */
public class FileDataSource implements DataSource {

    private String name;

    public FileDataSource(String name) {
        this.name = name;
    }

    @Override
    public void writeData(String data) {
        File file=new File(name);
        try (
                OutputStream fos=new FileOutputStream(file)
        ){
            fos.write(data.getBytes(),0,data.length());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String readData() {
        char[] buffer=null;
        File file=new File(name);
        try {
            FileReader fileReader=new FileReader(file);
            buffer=new char[(int) file.length()];
            fileReader.read(buffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(buffer);
    }
}
